package com.teach.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teach.mapper.ClassMapper;
import com.teach.mapper.CourseMapper;
import com.teach.mapper.ScoreMapper;
import com.teach.pojo.Course;
import com.teach.pojo.Score;
import com.teach.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService{
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private ClassMapper classMapper;

    @Override
    public List<Course> getCoursesByClass(Long classId) {
        return  courseMapper.getCoursesByClass(classId);
    }

    @Override
    public List<User> getUserByClassAndCourse(Long classId, Long courseId) {
        List<User> users = courseMapper.getUserByClassAndCourse(classId, courseId);
        if (users == null) {
            return null;
        }
        users.forEach(user->{
            Score score = scoreMapper.selectOne(new QueryWrapper<Score>().eq("user_id", user.getId()).eq("course_id", courseId));
            if (score != null) {
                user.setNum(score.getNum());
            }else {
                user.setNum(0);
            }
            user.setClassId(classId);
            user.setClassName(classMapper.selectById(classId).getClassName());
            user.setCourseId(courseId);
            user.setCourseName(courseMapper.selectById(courseId).getCourseName());
        });
        return users;
    }
}
